<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2017/11/23 0023

 * Time: 15:15

 */



namespace  app\mobile\controller;

use think\Db;

use think\Controller;





class Anastyle extends Bases

{



    public function _initialize(){

        if(!session('id')){

            $this->redirect('/admin/index/index');

        }

    }



    //客照列表

    public function index(){

        header("Cache-control: private");

        $is_recom=input('param.is_recom');





       $where['is_del'] = 0;

        $where['sign'] = "mobile";

       $data['list'] = Db::name('anastyle')->alias('a')



           ->where($where)

           ->order('a.sort desc,a.id desc')


           ->select();


        $this->assign('data',$data);

        return $this->fetch();

    }

    //分类

    public function vclass(){

//            $where['id']=0;

//            $where['sign']="mobile";

        $data = Db::name('worksclass')->where('show',1)->order('class_id desc')->select();

        $this->assign('data',$data);



        return $this->fetch();

    }

    public function add_vclass(){

        $id= intval(input('param.id'));

        if(request()->isAjax()){

            $data=[

                'name'=>input('param.name'),

                'show'=>intval(input('param.show'))

            ];

            $id= intval(input('param.id'));

            if($id> 0){

                $result = Db::name('worksclass')->where('class_id',$id)->update($data);

            }else{

                $result = Db::name('worksclass')->insert($data);

            }

            if($result !==false){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }



        }

        if($id){

            $data = Db::name('worksclass')->where('class_id',$id)->find();

            $this->assign('data',$data);

        }

        return $this->fetch();

    }

    //删除

    public function jddel(){

        $id = intval(input('post.id'));

        if($id){

            $res = Db::name('worksclass')->where('class_id',$id)->delete();

        }

        if($res){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }



    //按钮状态

    public function start(){

//        $is_recom = (int)input('post.is_recom');

        $id = (int)input('post.id');

        if($id){

            $info=Db::name('anastyle')->where('id',$id)->find();

            if ($info['is_recom']==1){

                $data['is_recom']=0;

            }else{

                $data['is_recom']=1;

            }

            $result = Db::name('anastyle')->where('id',$id)->update($data);

            if($result){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }

        }else{

            return json(['code'=>1,'msg'=>'数据异常']);

        }

    }

    //添加客照

    public function add_works()

    {



        $imgids_1 = Db::name('anastyle')->field('works_photo')->select();

        $imgids_2 = Db::name('zp')->field('works_photo')->select();

        $imgids_3 = Db::name('hxzp')->field('works_photo')->select();

        $ids = "";

        foreach ($imgids_1 as $v) {

            $ids .= $v['works_photo'] . ',';

        }

        foreach ($imgids_2 as $v) {

            $ids .= $v['works_photo'] . ',';

        }

        foreach ($imgids_3 as $v) {

            $ids .= $v['works_photo'] . ',';

        }

        $id_x = Db::name('works_pic')->where('pic_id', 'in', $ids)->select();

        //清除不存在的图片

        $dir = "./uploads/workss/";  //要获取的目录

        //先判断指定的路径是不是一个文件夹

        if (is_dir($dir)) {

            if ($dh = opendir($dir)) {

                while (($file = readdir($dh)) !== false) {

                    //文件名的全路径 包含文件名

                    $filePath = $dir . $file;

                    if (is_dir($filePath)) {

                        if ($dh_2 = opendir($filePath)) {

                            while (($file_2 = readdir($dh_2)) != false) {

                                $filePath_2 = $filePath . '/' . $file_2;

                                if (strpos($filePath_2, '.jpg')) {

                                    $filePath_3[] = $filePath_2;

                                }

                            }

                            closedir($dh_2);

                        }

                    }

                }

                closedir($dh);

            }

        }

        $ext=[];

        foreach ($id_x as $k => $value) {

            $ext[$k]='.'.str_replace('\\', '/', $value['works_pic']);

        }

        foreach ($filePath_3 as $v) {

                if(!in_array($v, $ext)) {

                    unlink($v);

                }

        }

        $info=Db::name('worksclass')->select();

        $this->assign('info',$info);

        return $this->fetch();

    }

    //编辑

    public function edit_works(){

        $id = input('param.id');

        if($id){

            $data =Db::name('anastyle')->where('id',$id)->find();

            $works_pic = Db('works_pic')->where('pic_id','in',$data['works_photo'])->field('works_pic,pic_id')->select();

            $data['content'] = html_entity_decode($data['content']);

            $this->assign('data',$data);

            $this->assign('imgurl',$works_pic);

        }

        return $this->fetch();

    }

    //确认提交

    public function addworks(){

        $id = input('post.id');

        $array_works_pic= isset($_POST['works_pic'])?implode(',',$_POST['works_pic']):'';

        $data=[

            'title'=>input('post.title'),
            
            'side'=>input('post.side'),

            'ps_time'=>strtotime(input('post.ps_time')),

            'class_id'=>intval(input('post.class_id')),

            'week'=>intval(input('post.week')),

            'is_show'=>intval(input('post.is_show')),

            'is_recom'=>intval(input('post.is_recom')),

            'sort'=>intval(input('post.sort')),

            'works_photo'=>$array_works_pic,

            'content'=>input('post.content'),

            'click'=>intval(input('post.click')),

            'seo_title'=>input('param.seo_title'),

            'seo_keywords'=>input('param.seo_keywords'),



            'sign'=>"namespace  app\mobile\controller"

        ];

        //检查时间

        // if($data['ps_time']){



        //     $date_now=date('j',$data['ps_time']); //得到是几号

        //     $cal_result=ceil($date_now/7); //计算是第几个星期几

        //     $month = date('Y-m',$data['ps_time']);//得到是几月

        //     if($id){

        //         $sql ="SELECT CEIL(FROM_UNIXTIME(ps_time,'%d')/7) zhou FROM  `dh_works`  WHERE FROM_UNIXTIME(ps_time,'%Y-%m')='$month' and id != $id and sign='pc'";

        //     }else{

        //         $sql ="SELECT CEIL(FROM_UNIXTIME(ps_time,'%d')/7) zhou FROM  `dh_works`  WHERE FROM_UNIXTIME(ps_time,'%Y-%m')='$month' and sign='pc'";

        //     }

        //     $week = Db::name('works')->query($sql);

        //     $week2= array();

        //     foreach ($week  as $k=>$v){

        //         foreach ($v as $m => $n) {

        //             $week2[] = $n;

        //         }

        //     }

        //     if(in_array($cal_result,$week2)){

        //         return json(['code'=>0,'msg'=>"错误信息：".date("Y年m月",$data['ps_time'])."的第".$cal_result."周的拍摄时间已存在"]);

        //     }



        // }



        $file_main_img = request()->file('main_img');

        if($file_main_img){

            $file_info_main_img = $file_main_img->move('./uploads/anastyle/');

            if($file_info_main_img){

                $data['main_img'] = '/uploads/anastyle/'.$file_info_main_img->getSaveName();

            }else{

                return json(['code'=>0,'msg'=>$file_main_img->getError()]);

            }

        }



        $file_seo_description = request()->file('seo_description');

        if($file_seo_description){

            $file_info_main_img = $file_seo_description->move('./uploads/anastyle/');

            if($file_info_main_img){

                $data['seo_description'] = '/uploads/anastyle/'.$file_info_main_img->getSaveName();

            }else{

                return json(['code'=>0,'msg'=>$file_seo_description->getError()]);

            }

        }

        if($id){

            if($file_seo_description){

                $old_img = Db::name('anastyle')->where('id',$id)->value('seo_description');

                if($old_img){

                    unlink('.'.str_replace('\\','/',$old_img));

                }

            }

            if($file_main_img){

                $old_img = Db::name('anastyle')->where('id',$id)->value('main_img');

                if($old_img){

                    unlink('.'.str_replace('\\','/',$old_img));

                }

            }

            $data['update_time']=time();

            $result = DB::name('anastyle')->where('id',$id)->update($data);

        }else{

            $data['create_time']=time();

            $result = Db::name('anastyle')->insert($data);

        }

        if($result !==false){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }

    //删除

    public function worksdel(){

        $id = intval(input('post.id'));

        if($id){

            $res = Db::name('anastyle')->where('id',$id)->update(['is_del'=>1]);;

        }

        if($res){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }

    //改变排序

    public function ChangeSort(){

        $sort = (int)input('post.sort');

        $id = (int)input('post.id');

        if($id){

            $data['sort']=$sort;

            $result = Db::name('anastyle')->where('id',$id)->update($data);

            if($result){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }

        }else{

            return json(['code'=>1,'msg'=>'数据异常']);

        }



    }



    //多图上传

    public function addworksajax(){

        $work_pic = request()->file('file');

        if(isset($work_pic)){

            $work_pic_file_info = $work_pic->move('./uploads/workss/');

            if ($work_pic_file_info) {

                $data['works_pic'] = '/uploads/workss/'.$work_pic_file_info->getSaveName();

                if($data){

                    $result = Db::name("works_pic")->insert($data);

                    $id = Db::name('works_pic')->getLastInsID();

                    $re = [

                        'status' => 200,

                        'info' => $id ,

                        'fileName'=>$data['works_pic'],

                    ];

                    if($result){

                        return json($re);

                    }

                }

            }

        }

    }

    //移除多图

    public function delworksajax(){

        $id = intval(input('param.id'));

        if($id>0){

            $url = Db::name('works_pic')

                ->where('pic_id',$id)

                ->value('works_pic');

            if($url){

                unlink(str_replace('\\','/',$_SERVER['DOCUMENT_ROOT'].$url));



            }

            $result = Db::name("works_pic")

                ->where('pic_id',$id)

                ->delete();

            if($result){

                return json(['status' => 200, 'info' => '移除成功']);

            }else{

                return  json(['status' => 400, 'info' => '移除失败']);

            }

        }else{

            return json(['status' => 400, 'info' => '数据异常']);

        }

    }



    public function hx(){

        $where['is_del']=0;

        $where['sign']="mobile";

        $data = Db::name('hxzp')->where($where)->order('sort desc,id desc')->select();

        $this->assign('data',$data);



        return $this->fetch();

    }

    public function add_hx(){



        return $this->fetch();

    }

    public function edit_hx(){

        $id = input('param.id');

        if($id){

            $data =Db::name('hxzp')->where('id',$id)->find();

            $works_pic = Db('works_pic')->where('pic_id','in',$data['works_photo'])->field('works_pic,pic_id')->select();

            $data['content'] = html_entity_decode($data['content']);

            $this->assign('data',$data);

            $this->assign('imgurl',$works_pic);

        }

        return $this->fetch();

    }

    //确认提交

    public function addhx(){

        $id = input('post.id');

        $array_works_pic= isset($_POST['works_pic'])?implode(',',$_POST['works_pic']):'';

        $data=[

            'title'=>input('post.title'),

            'ps_time'=>strtotime(intval(input('post.ps_time'))),

            'is_show'=>intval(input('post.is_show')),

            'sign'=>"mobile",

            'sort'=>intval(input('post.sort')),

            'works_photo'=>$array_works_pic,

            'content'=>input('post.content'),

            'click'=>intval(input('post.click')),

            'seo_title'=>input('param.seo_title'),

            'seo_keywords'=>input('param.seo_keywords'),

            'seo_description'=>input('param.seo_description'),

        ];

        $file_main_img = request()->file('main_img');

        if($file_main_img){

            $file_info_main_img = $file_main_img->move('./uploads/anastyle/');

            if($file_info_main_img){

                $data['main_img'] = '/uploads/anastyle/'.$file_info_main_img->getSaveName();

            }else{

                return json(['code'=>0,'msg'=>$file_main_img->getError()]);

            }

        }

        if($id){

            if($file_main_img){

                $old_img = Db::name('hxzp')->where('id',$id)->value('main_img');

                if($old_img){



                    unlink('.'.str_replace('\\','/',$old_img));

                }

            }

            $data['update_time']=time();

            $result = DB::name('hxzp')->where('id',$id)->update($data);

        }else{

            $data['create_time']=time();

            $result = Db::name('hxzp')->insert($data);

        }

        if($result !==false){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }



    //删除

    public function hxdel(){

        $id = intval(input('post.id'));

        if($id){

            $res = Db::name('hxzp')->where('id',$id)->update(['is_del'=>1]);;

        }

        if($res){

            return json(['code'=>1,'msg'=>'操作成功']);

        }else{

            return json(['code'=>0,'msg'=>'操作失败']);

        }

    }





    //改变排序

    public function ChangehxSort(){

        $sort = (int)input('post.sort');

        $id = (int)input('post.id');

        if($id){

            $data['sort']=$sort;

            $result = Db::name('hxzp')->where('id',$id)->update($data);

            if($result){

                return json(['code'=>1,'msg'=>'操作成功']);

            }else{

                return json(['code'=>0,'msg'=>'操作失败']);

            }

        }else{

            return json(['code'=>1,'msg'=>'数据异常']);

        }



    }



}